//
//  QMessageListCell.m
//  QoilPublic
//
//  Created by 杨凯 on 2017/7/20.
//  Copyright © 2017年 杨凯. All rights reserved.
//

#import "QMessageListCell.h"
@interface QMessageListCell ()
@property (weak, nonatomic) IBOutlet UILabel *titleLab;
@property (weak, nonatomic) IBOutlet UILabel *contentLab;
@property (weak, nonatomic) IBOutlet UILabel *orderIdLab;
@property (weak, nonatomic) IBOutlet UILabel *timeLab;
@property (weak, nonatomic) IBOutlet UILabel *roundLab;

@end
@implementation QMessageListCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    _roundLab.layer.cornerRadius = 5;
    _roundLab.layer.masksToBounds = YES;
}

- (void)setListModel:(QMsgListModel *)listModel{

    _listModel = listModel;
    if ([_listModel.ptId isEqualToString:@"1"]) {
        _orderIdLab.hidden = NO;
        _orderIdLab.text = _listModel.briefData;
        
    }else{
        _orderIdLab.hidden = YES;
    }
    _titleLab.text = _listModel.title;
    _contentLab.text = _listModel.data;
   
    if (_listModel.addTime.length>0) {
        
        NSString *currentDay = [self compareCurrentTime:_listModel.addTime];
        NSLog(@"currentDay:%@",currentDay);
//        _timeLab.text =[[[_listModel.addTime componentsSeparatedByString:@" "] lastObject] substringWithRange:NSMakeRange(0, 5)];
        _timeLab.text = currentDay;
    }
    
    _roundLab.hidden = YES;

    
}

- (NSString *) compareCurrentTime:(NSString *)str
{
    
    //把字符串转为NSdate
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
//    [dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *timeDate = [dateFormatter dateFromString:str];
    
    //得到与当前时间差
    NSTimeInterval  timeInterval = [timeDate timeIntervalSinceNow];
    timeInterval = -timeInterval;
    //标准时间和北京时间差8个小时
//    timeInterval = timeInterval - 8*60*60;
    long temp = 0;
    NSString *result;
    if (timeInterval < 60) {
        result = [NSString stringWithFormat:@"刚刚"];
    }
    else if((temp = timeInterval/60) <60){
        result = [NSString stringWithFormat:@"%ld分钟前",temp];
    }
    
    else if((temp = temp/60) <24){
        result = [NSString stringWithFormat:@"%ld小时前",temp];
    }
    
    else if((temp = temp/24) <30){
        result = [NSString stringWithFormat:@"%ld天前",temp];
    }
    
    else if((temp = temp/30) <12){
        result = [NSString stringWithFormat:@"%ld月前",temp];
    }
    else{
        temp = temp/12;
        result = [NSString stringWithFormat:@"%ld年前",temp];
    }
    
    return  result;
}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
